package com.zjut.zs.pay.manager;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;

import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Status;
import com.zjut.an.util.tools.DateUtil;
import com.zjut.an.util.tools.MD5;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.pay.dao.ZsPay;
import com.zjut.zs.pay.dao.ZsPayDAO;


public class ZsPayManager {

	@Resource(name = "ZsPayDAO")
	private ZsPayDAO dao;

	@Resource(name = "MD5")
	private MD5 md5;

	@Resource(name = "DateUtil")
	private DateUtil dateutil;
	
	@Resource(name = "AnConfigManager")
	private AnConfigManager anconfig;
	
	public ZsPay addData(ZsUser user, String payWay) {
		ZsPay obj = new ZsPay();
		try {
			List list = this.getDataByUser(user, payWay);
			System.out.println("订单数量:"+list.size());
			if(list.size()>0) {
				obj = (ZsPay)list.get(0);
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				Date olddate = format.parse(obj.getOrderTime());
				Date newdate = new Date();
				Calendar c1 = Calendar.getInstance();
		        Calendar c2 = Calendar.getInstance();
		        c1.setTime(olddate);
		        c2.setTime(newdate);
		        System.out.println("现在时间："+(c2.get(Calendar.MINUTE) +" 订单时间："+ c1.get(Calendar.MINUTE)) );
		        int timelong = ((c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR))*24*60 + (c2.get(Calendar.HOUR) - c1.get(Calendar.HOUR))*60 +(c2.get(Calendar.MINUTE) - c1.get(Calendar.MINUTE)));
		        System.out.println("时间间隔："+timelong);
		        int mit = 12*60;//默认一天
		        String mits = anconfig.getSingleConfigInfoByName("订单过期分钟");
		        if(null!=mits && mits.trim().length()>0){
		        	mit = Integer.parseInt(mits);
		        }
		        if(timelong>mit) {//超过时间未支付生成新订单
		        	obj = this.addNewPay(user, payWay);
		        }
			} else {
				obj = this.addNewPay(user, payWay);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return obj;
	}
	
	private ZsPay addNewPay(ZsUser user, String payWay) {
		ZsPay obj = new ZsPay();
		obj.setUsId(user.getUsId());
		obj.setUsIdcard(user.getUsIdcard());
		obj.setUsName(user.getUsName());
		obj.setPayType(anconfig.getSingleConfigInfoByName("订单名称")+"_"+user.getUsName()+user.getUsIdcard());
		obj.setOutTradeNo(dateutil.getCurrentDate4()+UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4));//重要修改，去掉订单中usid
		obj.setPayWay(payWay);
		obj.setTotalFee(anconfig.getSingleConfigInfoByName("缴费金额"));
		obj.setOrderTime(dateutil.getCurrentDate5());
		obj.setPayEnd(Status.PAYSTATUS_NULL.getStatus());
		this.addData(obj);
		return obj;
	}
	
	public void addData(ZsPay obj) {	
		dao.save(obj);
	}
	
	public void updateData(ZsPay obj) {
		dao.update(obj);
	}
	
	public void deleteData(ZsPay obj) {
		dao.delete(obj);
	}

	public void deleteDatas(List<ZsPay> objlist) {
		for (ZsPay ZsPay : objlist) {
			dao.delete(ZsPay);
		}
	}
	
	public PageModule getDataByInstance (int pageNumber,int pageSize, String sort, String order, ZsPay obj) {
		return dao.findByInstance(pageNumber, pageSize, sort, order, obj);
	}
	
	public List getDatas() {
		return dao.findAll();
	}

	public ZsPay getData(int objId) {
		return dao.findById(objId);
	}
	
	public List getDataByProperty(String propertyName, Object value){
		return dao.findByProperty(propertyName,value);
	}
	
	public List getDataByUser(ZsUser user, String payWay) {
		ZsPay obj = new ZsPay();
		obj.setUsId(user.getUsId());
		obj.setPayWay(payWay);
		obj.setPayEnd(Status.PAYSTATUS_NULL.getStatus());
		System.out.println("查找订单："+user.getUsId()+" "+payWay+" "+Status.PAYSTATUS_NULL.getStatus());
		return this.getDataByInstance(1, 1000, null, null, obj).getObjects();
	}

	public List getDatasByUser(ZsUser user) {
		ZsPay obj = new ZsPay();
		obj.setUsId(user.getUsId());
		return this.getDataByInstance(1, 1000, null, null, obj).getObjects();
	}
}
